
package bibliotheque.client.gui;

import bibliotheque.client.Tools;
import bibliotheque.events.PingResultOccuredEvent;
import bibliotheque.events.PingResultOccuredListener;
import bibliotheque.nameserver.parser.Graphe;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JPanel;
import javax.swing.text.html.BlockView;

/**
 *
 * @author guillaume
 */
public class ToolsGui extends JPanel implements PingResultOccuredListener {
    
    //private
    private Tools tools;
    
    private javax.swing.JButton jButtonPing;
    private javax.swing.JButton jButtonGraphe;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTextArea jTextArea1;
    
    public ToolsGui(Tools tools) {
        this.initcomponents();
        this.tools = tools;
        this.tools.AddPingResultOccuredListener(this);
    }
    
    public void initcomponents() {
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jPanel1 = new javax.swing.JPanel();
        jButtonPing = new javax.swing.JButton();
        jButtonGraphe = new javax.swing.JButton();
        
        
        // default grid : one box per component, on a single row
        this.setLayout(new GridLayout());
        
        this.add(this.jSplitPane1);
//        jTextArea1.setColumns(20);
//        jTextArea1.setRows(5);
        this.jScrollPane1.setViewportView(jTextArea1);
        this.jScrollPane1.setAutoscrolls(true);
        
        
        
        jButtonPing.setText("Ping");
        jButtonPing.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                //TODO : listener for ping action
                ToolsGui.this.NewPing();
            }
        });
        this.jButtonPing.setMinimumSize(new Dimension(200,20));
        
        jButtonGraphe.setText("Network Graph");
        jButtonGraphe.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                //TODO : listener for ping action
                ToolsGui.this.GetGraph();
            }
        });
        
        this.jPanel1.setLayout(new GridLayout(10,2));
        this.jPanel1.add(this.jButtonPing);
        this.jPanel1.add(this.jButtonGraphe);
        
        this.jSplitPane1.setLeftComponent(this.jPanel1);
        this.jSplitPane1.setRightComponent(this.jScrollPane1);
        
    }
    
    public void PingOccured(PingResultOccuredEvent event) {
        //TODO
        //this.jScrollPane1.removeAll();
        //this.jScrollPane1.add(this.jScrollPane1);
        this.jSplitPane1.setRightComponent(this.jTextArea1);
        this.jTextArea1.setText("PING RESULTS :\n");
        this.jTextArea1.append("____________________________________________________________________\n");
        Iterator<String> iter = event.GetPingList().iterator();
        while (iter.hasNext()) {
            this.jTextArea1.append(iter.next() + "\n");
        }
        
        this.jTextArea1.append("\n\n");
        
        
        //System.out.println("Ping revenu");
    }
    
    protected void NewPing() {
        this.tools.Ping();
    }
    
    protected void GetGraph() {
        //this.jScrollPane1.removeAll();
        //this.jScrollPane1.add(new Graphe(this.tools.GetGraphe()));
        this.jSplitPane1.setRightComponent(new Graphe(this.tools.GetGraphe()));
    }
}
